//
//  AXHomeZxktVC.m
//  AnXin100
//
//  Created by 天质弘耕 on 2018/10/22.
//  Copyright © 2018年 tzhg. All rights reserved.
//在线课堂首页

#import "AXHomeZxktVC.h"
#import "LKCircleScrollView.h"
#import "Header.h"
#import "ZXCategories.h"
#import "UIView+SDAutoLayout.h"
#import "AXHomeZxktFirstCell.h"
#import "AXHomeZxktSecondCell.h"
#import "AXHomeZxktSecondModel.h"
#import "UIView+SDAutoLayout.h"
#import "ZXCategories.h"
#import "UITableView+SDAutoTableViewCellHeight.h"
#import "ClassroomViewController.h"
#import "AXZxktJpktVC.h"
#import "AXZxktNjktVC.h"
#import "AXZxktDksnVC.h"
#import "AXZxktZxflVC.h"



@interface AXHomeZxktVC ()<UITableViewDelegate,UITableViewDataSource,LKCircleScrollViewDelegate, LKCircleScrollViewDataSource,UISearchBarDelegate>



/** headView */
@property (nonatomic ,strong) UIView *headView;
@property (nonatomic, strong) UITableView *tableView;
/** 广告图片 */
@property (nonatomic ,strong) UIImageView *advImageView;
/** 循环滚动View */
@property (nonatomic, strong) LKCircleScrollView *circleScrollView;
@property (nonatomic, strong) NSArray<NSString *> *scrollViewImageNames;
@property (nonatomic, strong) NSMutableArray<AXHomeZxktSecondModel *>  *modelsArray;

@end

@implementation AXHomeZxktVC




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatView];
    [self setlayout];
    
    self.tableView.tableHeaderView = _headView;
    [_headView setNeedsLayout];
    [_headView layoutIfNeeded];//刷新布局
    CGFloat height = [_headView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    _headView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, height);//headView 的大小
    _headView.backgroundColor = [UIColor redColor];
    self.tableView.tableHeaderView = _headView;
    
    
   
    

    
}

- (void)creatView {
    
    _headView = [[UIView alloc]init];
    
    //添加scrollview
    self.scrollViewImageNames = @[@"shcm", @"sg2.jpeg.jpeg", @"sg3.jpeg.jpeg"];
    //self.circleScrollView = [[LKCircleScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
    self.circleScrollView.isAutoScroll = NO;
    self.circleScrollView = [[LKCircleScrollView alloc]init];
    self.circleScrollView.isAutoScroll = NO;
    self.circleScrollView.delegate = self;
    self.circleScrollView.dataSource = self;
    [self.headView addSubview:self.circleScrollView];
    
    
    self.tableView = [[UITableView alloc]init];
    /** 去掉cell线 */
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    //注册cell
    [self.tableView registerClass:[AXHomeZxktFirstCell class] forCellReuseIdentifier:[AXHomeZxktFirstCell reuseID]];
    [self.tableView registerClass:[AXHomeZxktSecondCell class] forCellReuseIdentifier:[AXHomeZxktSecondCell reuseID]];

    _headView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 129);
    self.tableView.tableHeaderView = self.headView;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
}

- (void)setlayout {
    
    if (_isHeight) {
        self.tableView.sd_layout
        .leftSpaceToView(self.view, 0)
        .topSpaceToView(self.view, 0)
        .widthIs(Wi)
        .heightIs(SCREEN_HEIGHT);
    } else {
        self.tableView.sd_layout
        .leftSpaceToView(self.view, 0)
        .topSpaceToView(self.view, 0)
        .widthIs(Wi)
        .heightIs(Hi-Hs-Htb-44);
    }

    
    self.circleScrollView.sd_layout
    .leftSpaceToView(self.headView,0)
    .rightSpaceToView(self.headView,0)
    .topSpaceToView(self.headView,0)
    .heightIs(129);
    
    [_headView setupAutoHeightWithBottomView:self.circleScrollView bottomMargin:0];
    
}


#pragma mark - tableViewDelegate & tableViewDatasorce
/** cell 的点击方法 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ClassroomViewController *vc = [[ClassroomViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    
}

//几个分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        
        return 1;
    }
    return 10;
}

/** cell的高度 */

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        return 100;
        
    }
    
     return [tableView cellHeightForIndexPath:indexPath model:self.modelsArray[indexPath.row] keyPath:@"model" cellClass:[AXHomeZxktSecondCell class] contentViewWidth:SCREEN_WIDTH];
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        AXHomeZxktFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:[AXHomeZxktFirstCell reuseID] forIndexPath:indexPath];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        cell.tapButtonClickedBlock = ^(NSInteger index) {
            //全部宝贝、优先体验
            if (index == 0) {
                
                AXZxktJpktVC *vc = [[ AXZxktJpktVC alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
            }else if (index ==1) {
                
                AXZxktNjktVC *vc = [[AXZxktNjktVC alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
                
                
            }else if (index == 2){
                
                AXZxktDksnVC *vc = [[ AXZxktDksnVC alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
                
            }
            else if (index == 3){
                
                AXZxktZxflVC *vc = [[AXZxktZxflVC alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
                
            }
        };
        
        return cell;
    }
    AXHomeZxktSecondCell *cell = [tableView dequeueReusableCellWithIdentifier:[AXHomeZxktSecondCell reuseID] forIndexPath:indexPath];
    cell.productImageView.tag = indexPath.row;
    
  
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    cell.tapButtonClickedBlock = ^(NSInteger index) {
        
        ClassroomViewController *vc = [[ClassroomViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
      

    };
    

    
    return cell;
    
    
}



#pragma mark - Circle Scroll View Data Source & Delegate

- (NSInteger)numberOfViewsInCircleScrollView:(LKCircleScrollView *)circleScrollView {
    
    return self.scrollViewImageNames.count;
}

/** 视图来源 */
- (UIView *)circleScrollView:(LKCircleScrollView *)circleScrollView viewAtIndex:(NSInteger)index {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.scrollViewImageNames[index]]];
    imageView.contentMode =UIViewContentModeScaleAspectFill;
    imageView.clipsToBounds=YES;//
    
    if (index == 0) {
        
        imageView.userInteractionEnabled = YES;
        
        
        
    }
    
    
    return imageView;
}

/** 点击View时调用  */
- (void)circleScrollView:(LKCircleScrollView *)circleScrollView didTapViewAtIndex:(NSInteger)index {
    if (index == 0) {
        //事件不会触发，点击事件可能被ScrollView截获
        //        ZXVoteProductionListViewController *vc = [[ZXVoteProductionListViewController alloc] init];
        //        [self.navigationController pushViewController:vc animated:YES];
    }
}





- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    
}


#pragma mark - UISearchBar delegate
//将要开始编辑时的回调，返回为NO，则不能编辑
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    
    return NO;
}


@end



